package com.example.devinfan.criminalintent.model;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;

public class CriminalIntentJSONSerialier {
    private Context mContext;
    private String mFilename;
    public CriminalIntentJSONSerialier(Context c, String f) {
        mContext = c;
        mFilename = f;
        //checkHaveExternalStorage();
    }

    public void saveCrimes(ArrayList<Crime> crimes) throws JSONException,IOException{
        //Build an array in JSON
        JSONArray array = new JSONArray();

        for(Crime c : crimes){
            array.put(c.toJSON());
        }

        Writer writer = null;
        try {
            OutputStream out = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE);
            writer = new OutputStreamWriter(out);
            writer.write(array.toString());
        }finally {
            if (writer != null){
                writer.close();
            }
        }
    }

    public ArrayList<Crime> loadCrimes() throws IOException,JSONException{
        ArrayList<Crime> crimes = new ArrayList<Crime>();
        StringBuilder JSONString = new StringBuilder();
        BufferedReader reader = null;
        try{
            //OPen and read the file into a stringBuilder
            InputStream in = mContext.openFileInput(mFilename);
            reader = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = reader.readLine()) != null) {
                //line breaks are omitted and irrelevant
                JSONString.append(line);
            }
            //Parse the JSON using JSONTokener
            JSONArray array = (JSONArray) new JSONTokener(JSONString.toString()).nextValue();
            //Build the array of crimes from JSONObjects
            for(int i = 0 ; i < array.length(); i++) {
                crimes.add(new Crime(array.getJSONObject(i)));

            }
        }catch (FileNotFoundException e){
            //Ignore this one ; it happens when starting fresh
        }finally {
            if(reader != null) {
                reader.close();
            }
        }
        return crimes;
    }

    /*  判断是否有外设SD卡，其实现在手机都有几G的内存，它这个方法直接当本机内存是外设SD卡
    private void checkHaveExternalStorage(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            mFilename = Environment.getExternalStorageDirectory().getPath()+File.separator+mFilename;
        }
    }
    如果想要外设SD卡储存的，就用这种
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),mFilename);
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(file));
            writer.write("I am superman");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (writer != null) try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
*/

}
